package com.mofeng.myshop.service.user;

import com.mofeng.myshop.constants.UserConstant;
import com.mofeng.myshop.domain.vo.LoginStateVo;
import com.mofeng.myshop.service.WebInfoService;
import com.mofeng.myshop.until.EmailSender;
import com.mofeng.myshop.until.MyUtil;
import com.mofeng.myshop.until.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;

@Service
@Slf4j
@RequiredArgsConstructor
public class EmailService {

    private final EmailSender emailSender;

    private final WebInfoService webInfoService;

    /**
     * @param id
     * @param message
     * @param title
     * @return
     */
    public R sendMessageToOne(String id, String message, String title) {
        return null;
    }

    /**
     * @param message
     * @param title
     * @return
     */
    public R sendMessageToAll(String message, String title) {
        return null;
    }

    /**
     * @param message
     * @return
     */
    public R subBug(String message) {
        return null;
    }


    /**
     * 发送手机验证码
     */
    @Async
    public void sendPhoneVerifyCode(String code, String phone, String type) {


    }

    /**
     * 发送邮箱验证码
     */
    @Async
    public void sendEmailVerifyCode(String code, String emailAddress, String type) {
        try {
            String title = getWebTitle();
            emailSender.subject(title + getCodeType(type))
                    .from(title + "系统")
                    .text(title + " 您的验证码：" + code +
                            "\n" +
                            "(" + title + "，此验证码5分钟内有效，如非本人操作请忽略此邮件！)")
                    .to(emailAddress)
                    .send();
        } catch (MessagingException e) {
            throw new RuntimeException("发送邮件出错，请检查后充实");
        }
    }

    /**
     * 发送邮件
     */
    @Async
    public void sendEmail(String title, String content, String emailAddress) {
        try {
            emailSender.subject(title)
                    .from(getWebTitle() + "系统")
                    .text(content)
                    .to(emailAddress)
                    .send();
        } catch (MessagingException e) {
            throw new RuntimeException("发送邮件出错，请检查后重试");
        }
    }

    /**
     * 发送邮件
     */
    @Async
    public void sendEmail(String form, String title, String content, String emailAddress) {
        try {
            emailSender.subject(getWebTitle() + "系统 --》 " + title)
                    .from(form)
                    .text(content)
                    .to(emailAddress)
                    .send();
        } catch (MessagingException e) {
            throw new RuntimeException("发送邮件出错，请检查后重试");
        }
    }

    private String getWebTitle() {
        String title = webInfoService.getWebTitle();
        return "【" + title + "】";
    }

    private String getCodeType(String type) {
        String result = "验证码";
        result = switch (type) {
            case UserConstant.Email.REGISTER_KEY -> "（注册）";
            case UserConstant.Email.UPDATE_KEY -> "（修改邮箱）";
            case UserConstant.Email.FORGET_KEY -> "（忘记密码）";
            default -> getWebTitle();
        };

        return result;
    }

    /**
     * 发送邮箱警报，有人尝试破解密码
     */
    @Async
    public void sendEmailWarning(LoginStateVo loginStateVo) {

    }
}
